//
var Dao = require('./DAO')
var cartDAO = {
  getAllCart: function (userId, callback) {
    Dao('SELECT cartID,sum,discount.discountID id,num,newprice newmoney,pastprice pastprice,disName cname,disimage1 cpic,discontent content FROM cart,discount where userId=? and type=1 and cart.discountID=discount.discountID union SELECT cartID,sum,trade.tradeID id,num,price newmoney,newprice pastprice,tradeName cname,images cpic,advantage content FROM cart,trade where userId=? and type=0 and cart.tradeID=trade.tradeID ', [userId, userId], function (err, results) {
      if (err) {
        callback(err, null)
      } else {
        callback(null, results)
      }
    })
  },
  // 单个删除购物车商品
  delAllCart: function (cartID, callback) {
    Dao('DELETE  FROM cart where cartID=?', [cartID], function (err, results) {
      if (err) {
        callback(err, null)
      } else {
        callback(null, results)
      }
    })
  },
  delAllCarts: function (cartID, callback) {
    Dao('DELETE  FROM cart where cartID in ('+cartID+') ', function (err, results) {
      if (err) {
        callback(err, null)
      } else {
        callback(null, results)
      }
    })
  },
  //根据type查询值
  getTypecart: function ([type, userId, discountID, tradeID], callback) {
    Dao('SELECT cartID FROM `cart` where type =? and userId=? and discountID=? and tradeID=? ', [type, userId, discountID, tradeID], function (err, results) {
      if (err) {
        callback(err, null)
      } else {

        callback(null, results)
      }
    })
  },
  // 购物车商品数量增加
  updateCartnum: function (cartID, callback) {
    console.log('dao', cartID)
    Dao('UPDATE cart set num=num+1,sum=sum+money where cartID=?', cartID, function (err, results) {
      if (err) {
        callback(err, null)
      } else {

        callback(null, results)
      }
    })
  },
  updateCartsubnum: function (cartID, callback) {
    console.log('dao', cartID)
    Dao('UPDATE cart set num=num-1,sum=sum-money where cartID=?', cartID, function (err, results) {
      if (err) {
        callback(err, null)
      } else {

        callback(null, results)
      }
    })
  },
  // 向购物车添加商品
  insertCart: function ([discountID, tradeID, type, userId, num, sum, money], callback) {

    Dao('INSERT into cart VALUES(null,?,?,?,?,?,?,?)', [discountID, tradeID, type, userId, num, sum, money], function (err, results) {
      if (err) {
        callback(err, null)
      } else {

        callback(null, results)
      }
    })
  },
  getcartID: function (cartID, callback) {

    Dao('SELECT num FROM `cart` where cartID=? ', cartID, function (err, results) {
      if (err) {
        callback(err, null)
      } else {
        callback(null, results)
      }
    })
  },



}

module.exports = cartDAO